#!/usr/bin/env python

import sys

sys.path.insert(0, '../')

import os

if not os.getenv('DJANGO_SETTINGS_MODULE'):
    os.environ['DJANGO_SETTINGS_MODULE'] = 'boxuegu.settings'

import django

django.setup()

from django.conf import settings
from django.template import loader
from courses.models import Course
from organization.models import CourseOrg
from users.models import Banner


def generate_static_index_html():
    all_banners = Banner.objects.all().order_by('index')
    courses = Course.objects.filter(is_banner=False)[:5]
    banner_courses = Course.objects.filter(is_banner=False)[:3]
    course_orgs = CourseOrg.objects.all()[:15]
    content = {
        'all_banners': all_banners,
        'courses': courses,
        'banner_courses': banner_courses,
        'course_orgs': course_orgs}
    # 获取模板
    templates = loader.get_template('index.html')
    # 渲染首页html字符串
    html_text = templates.render(content)
    file_path = os.path.join(settings.STATICFILES_DIRS[0], 'index.html')
    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(html_text)


if __name__ == '__main__':
    generate_static_index_html()
